using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Core;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Blazored.LocalStorage;
using AntDesign;
using IdentityServer4Blazor.Client.ApiRequest;
using Autofac.Extensions.DependencyInjection;
using Autofac;

namespace IdentityServer4Blazor.Client
{
    public class Program
    {
        public static async Task Main(string[] args)
        {

            var levelSwitch = new LoggingLevelSwitch();
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.ControlledBy(levelSwitch)
                .Enrich.WithProperty("InstanceId", Guid.NewGuid().ToString("n"))
                .WriteTo.BrowserConsole()
                .CreateLogger();
            try
            {
                var builder = WebAssemblyHostBuilder.CreateDefault(args);
                builder.RootComponents.Add<App>("app");
                builder.Services.AddAntDesign();
                builder.Services.AddBlazoredLocalStorage();
                builder.Services.AddTransient(sp => 
                {
                  return  new HttpClient()
                    {
                        BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
                    };
                });
                builder.ConfigureContainer(new AutofacServiceProviderFactory(Register));
                builder.Services.AddOptions();
                builder.Services.AddAuthorizationCore();
                builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthStateProvider>();
                builder.Logging.SetMinimumLevel(LogLevel.Debug);
                await builder.Build().RunAsync();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "An exception occurred while creating the WASM host");
                throw;
            }

        }
        private static void Register(ContainerBuilder builder)
        {
            builder.RegisterModule<ConfigureAutofac>();
            // add any registrations here
        }
    }
}
